/*  
 *  --[Ag_12] - Reading the Weather station at Agriculture v20 board-- 
 *  
 *  Explanation: Turn on the Agriculture v20 board and read the 
 *  Weather station on it once every second
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Manuel Calahorra
 */

#include <WaspSensorAgr_v20.h>

// Variable to store the number of anemometer pulses
long pluviometerCounter = 0;

// Variable to store the precipitations value
float pluviometer;

void setup()
{
  // Turn on the USB and print a start message
  USB.ON();
  USB.println(F("start"));
  delay(100);

  // Turn on the sensor board
  SensorAgrv20.ON();
  delay(100);
  
  // Turn on the RTC
  RTC.ON();
  delay(100);
  
}
 
void loop()
{
  // Part 1: Enabling Pluviometer Interruptions and sleeping
  SensorAgrv20.sleepAgr("00:00:00:30", RTC_OFFSET, RTC_ALM1_MODE3,UART0_OFF | UART1_OFF | BAT_OFF | RTC_OFF, SENS_AGR_PLUVIOMETER);
  
  // Part 2: After wake up update and print the rainfall detected

  // Turn on the USB
  USB.ON();
  
  // Process the interruption received
  if( intFlag & PLV_INT)
  {
    USB.println("Pluviometer interruption arrived");
    pluviometerCounter++;
  } else if(intFlag & RTC_INT)
  {
    USB.println("RTC interruption arrived");
  }

  // Convert the number of interruptions received into mm of rain
  pluviometer = float(pluviometerCounter) * 0.2794;  
  
  // Print the accumulated rainfall
  USB.print("Accumulated rainfall: ");
  USB.print(pluviometer);
  USB.println("mm");

  // Clearing the interruption flag before coming back to sleep
  clearIntFlag();
}
